package com.ruoyi.common.utils;

import org.apache.commons.compress.utils.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;

public class TxtUtils {
    private static final Logger LOG = LoggerFactory.getLogger(TxtUtils.class);
    public static final String CHAR_SET_NAME = "UTF-8";

    public static void main(String[] args) {
        String content = "2022 中国 ok";
        String filePath = "D:/home/txt/1.txt";
        boolean append = false;
        TxtUtils.write(content, filePath, append);
        System.out.println("append=" + append + "=====================" + TxtUtils.read(filePath).toString());


        String filePath2 = "D:/home/txt/2.txt";
        boolean append2 = true;
        for (int i = 0; i < 10; i++) {
            String content2 = "2022 中国 ok" + i;
            TxtUtils.write(content2, filePath2, append2);
        }
        System.out.println("append=" + append2 + "=====================" + TxtUtils.read(filePath2).toString());


        //System.out.println(TxtUtils.read("D:/home/license/license.dat").toString());
    }

    /**
     * 将数据写入到txt文件
     *
     * @param content  the content
     * @param filePath the file path
     */
    public static void write(String content, String filePath, boolean append) {
        File file = new File(filePath);
        BufferedWriter out = null;
        try {
            if (file.createNewFile()) {
                LOG.info("创建单个文件" + filePath + "成功!");
            }
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append), CHAR_SET_NAME));
            if (append) {
                content += "\r\n";
            }
            out.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 从txt文件中读取数据
     *
     * @param path the path
     * @return the list
     */
    public static List<String> read(String path) {
        List<String> result = Lists.newArrayList();
        File file = new File(path);
        if (!file.exists()) {
            return result;
        }
        BufferedInputStream fis = null;
        BufferedReader reader = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(file));
            // 用5M的缓冲读取文本文件
            reader = new BufferedReader(new InputStreamReader(fis, CHAR_SET_NAME), 5 * 1024 * 1024);
            String line = "";
            while ((line = reader.readLine()) != null) {
                result.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String readToStr(String path) {
        StringBuilder result = new StringBuilder();
        File file = new File(path);
        if (!file.exists()) {
            return result.toString();
        }
        BufferedInputStream fis = null;
        BufferedReader reader = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(file));
            // 用5M的缓冲读取文本文件
            reader = new BufferedReader(new InputStreamReader(fis, CHAR_SET_NAME), 5 * 1024 * 1024);
            String line = "";
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }


}
